{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0612c3d5-1d63-4d04-9ef7-873224f6b34b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from tempfile import TemporaryFile\n",
    "import tempfile\n",
    "from io import StringIO\n",
    "import functools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a10b2a5-b70b-4397-8431-230e523d47bb",
   "metadata": {},
   "source": [
    "# NumPy二进制文件（NPY，NPZ）\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "load(file[, mmap_mode, allow_pickle, …])|从.npy、.npz或pickle文件加载阵列或pickle对象。\n",
    "save(file, arr[, allow_pickle, fix_imports])|将数组保存为NumPy.npy格式的二进制文件。\n",
    "savez(file, *args, **kwds)|将几个数组以未压缩的.npz格式保存到单个文件中。\n",
    "savez_compressed(file, *args, **kwds)|以压缩的.npz格式将几个数组保存到单个文件中。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20498f65-c088-41bd-8545-2f646d8ba228",
   "metadata": {},
   "source": [
    "## numpy.load(file, mmap_mode=None, allow_pickle=False, fix_imports=True, encoding='ASCII', *, max_header_size=10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c88e090a-344f-41f3-8a1f-53574e54cd66",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.save('np123', np.array([[1, 2, 3], [4, 5, 6]]))\n",
    "np.load('np123.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "99f1c45d-26e8-4a0c-8d38-a8259b59d569",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.array([[1, 2, 3], [4, 5, 6]])\n",
    "b=np.array([1, 2])\n",
    "np.savez('np123.npz', a=a, b=b)\n",
    "data = np.load('np123.npz')\n",
    "data['a']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "22cc5d87-aa31-43a3-a695-7eab324b9768",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['b']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9d263429-bebb-45f9-be32-3cc58cd6b02b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "data.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "65a83c96-8e3a-469a-86f7-50c9d3cc7197",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "memmap([4, 5, 6])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.load('np123.npy', mmap_mode='r')\n",
    "X[1, :]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28bd99be-a446-4ffe-93f7-ae97bc136388",
   "metadata": {},
   "source": [
    "## numpy.save(file, arr, allow_pickle=True, fix_imports=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "831c6e8e-dd76-4adc-a1c7-7552249dd117",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outfile = TemporaryFile()\n",
    "x = np.arange(10)\n",
    "np.save(outfile, x)\n",
    "_ = outfile.seek(0)\n",
    "np.load(outfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "df77b2c1-7149-4d9b-8da0-11d4601c1636",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2] [1 3]\n"
     ]
    }
   ],
   "source": [
    "with open('test.npy', 'wb') as f:\n",
    "    np.save(f, np.array([1, 2]))\n",
    "    np.save(f, np.array([1, 3]))\n",
    "with open('test.npy', 'rb') as f:\n",
    "    a = np.load(f)\n",
    "    b = np.load(f)\n",
    "print(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f65b443c-04fb-442c-9535-6179f9b8ffc7",
   "metadata": {},
   "source": [
    "## numpy.savez(file, *args, **kwds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "024407ca-efa7-4aa2-a62b-975b0bf4ef05",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['arr_0', 'arr_1']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outfile = TemporaryFile()\n",
    "x = np.arange(10)\n",
    "y = np.sin(x)\n",
    "np.savez(outfile, x, y)\n",
    "_ = outfile.seek(0)\n",
    "npzfile = np.load(outfile)\n",
    "npzfile.files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8b699c82-9139-45b4-a10a-4a408a0370c8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "npzfile['arr_0']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a14a3609-9b3d-4928-a5f5-e8193d76762d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['x', 'y']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outfile = TemporaryFile()\n",
    "np.savez(outfile, x=x, y=y)\n",
    "_ = outfile.seek(0)\n",
    "npzfile = np.load(outfile)\n",
    "sorted(npzfile.files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f4ad7f5f-7ba8-4036-88a0-1bb152d7f1d4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "npzfile['x']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1767dedc-a0aa-4268-af1c-2415f8c81a69",
   "metadata": {},
   "source": [
    "## numpy.savez_compressed(file, *args, **kwds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3a2e15f2-6a77-4f3b-aa96-45f4b9b1efe9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "test_array = np.random.rand(3, 2)\n",
    "test_vector = np.random.rand(4)\n",
    "np.savez_compressed('np123', a=test_array, b=test_vector)\n",
    "loaded = np.load('np123.npz')\n",
    "print(np.array_equal(test_array, loaded['a']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "61531883-23de-4079-9c0b-33438c52cfbb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(np.array_equal(test_vector, loaded['b']))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e225189-702b-494c-94b2-ec49a7e15844",
   "metadata": {},
   "source": [
    "# 文本文件\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "loadtxt(fname[, dtype, comments, delimiter, …])|从文本文件加载数据。\n",
    "savetxt(fname, X[, fmt, delimiter, newline, …])|将数组保存到文本文件。\n",
    "genfromtxt(fname[, dtype, comments, …])|从文本文件加载数据，并按指定方式处理缺少的值。\n",
    "fromregex(file, regexp, dtype[, encoding])|使用正则表达式解析从文本文件构造数组。\n",
    "fromstring(string[, dtype, count, sep])|从字符串中的文本数据初始化的新一维数组。\n",
    "ndarray.tofile(fid[, sep, format])|将数组以文本或二进制形式写入文件(默认)。\n",
    "ndarray.tolist()|以Python标量的a.ndim级深嵌套列表的形式返回数组。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8251a356-8e3a-48b1-b0f6-6e5403ed5bc4",
   "metadata": {},
   "source": [
    "## numpy.loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None, *, quotechar=None, like=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "85d931b0-fe34-4eba-983d-cd48e36cfc16",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 1.],\n",
       "       [2., 3.]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = StringIO(\"0 1\\n2 3\")\n",
    "np.loadtxt(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "08c5478f-81e8-4f71-9874-21f55f90fd6a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([(b'M', 21, 72.), (b'F', 35, 58.)],\n",
       "      dtype=[('gender', 'S1'), ('age', '<i4'), ('weight', '<f4')])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = StringIO(\"M 21 72\\nF 35 58\")\n",
    "np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),\n",
    "                     'formats': ('S1', 'i4', 'f4')})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2f71d6e5-23eb-4a2d-86ec-f5ee11f9b459",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1., 3.]), array([2., 4.]))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = StringIO(\"1,0,2\\n3,0,4\")\n",
    "x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)\n",
    "x, y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "07fbac42-ee8d-448f-9b35-e17838bb4780",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 3.],\n",
       "       [3., 5.]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = StringIO(\"1.618, 2.296\\n3.141, 4.669\\n\")\n",
    "conv = {\n",
    "    0: lambda x: np.floor(float(x)),\n",
    "    1: lambda x: np.ceil(float(x)),\n",
    "}\n",
    "np.loadtxt(s, delimiter=\",\", converters=conv)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b7b9551-72d6-4515-84c4-b7ded65940be",
   "metadata": {},
   "source": [
    "## numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\\n', header='', footer='', comments='# ', encoding=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1c5b7d17-81e5-49ed-806a-1db9ae2685bd",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "x = y = z = np.arange(0.0,5.0,1.0)\n",
    "np.savetxt('test.out', x, delimiter=',')\n",
    "np.savetxt('test.out', (x,y,z))\n",
    "np.savetxt('test.out', x, fmt='%1.4e')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5978b48f-9792-400a-9947-e4093bfa9155",
   "metadata": {},
   "source": [
    "## numpy.genfromtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=\" !#$%&'()*+, -./:;<=>?@[\\\\]^{|}~\", replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None, encoding='bytes', *, ndmin=0, like=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "08d1faee-ecc0-45c8-afc4-31e46e3a3c99",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array((1, 1.3, b'abcde'),\n",
       "      dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', 'S5')])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = StringIO(u\"1,1.3,abcde\")\n",
    "data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),\n",
    "('mystring','S5')], delimiter=\",\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8264403b-1c9f-4a93-99fc-6e950aa1ee4a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array((1, 1.3, b'abcde'),\n",
       "      dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', 'S5')])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_ = s.seek(0)\n",
    "data = np.genfromtxt(s, dtype=\"i8,f8,S5\",\n",
    "names=['myint','myfloat','mystring'], delimiter=\",\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "de766ba4-594d-4214-802e-86685e38291c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([(b'text', b''), (b'hello world', b'11'), (b'numpy', b'5')],\n",
       "      dtype=[('f0', 'S12'), ('f1', 'S12')])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = StringIO('''\n",
    "text,# of chars\n",
    "hello world,11\n",
    "numpy,5''')\n",
    "np.genfromtxt(f, dtype='S12,S12', delimiter=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6ad8aab-5c01-4bbd-912f-5628a72eeb56",
   "metadata": {},
   "source": [
    "## numpy.fromregex(file, regexp, dtype, encoding=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "88022cc6-aabb-435f-ad84-0ae008389c05",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "text = StringIO(\"1312 foo\\n1534  bar\\n444   qux\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1a370d1c-0abc-4341-8354-1409883e7bab",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([(1312, b'foo'), (1534, b'bar'), ( 444, b'qux')],\n",
       "      dtype=[('num', '<i8'), ('key', 'S3')])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regexp = r\"(\\d+)\\s+(...)\"  # match [digits, whitespace, anything]\n",
    "output = np.fromregex(text, regexp,\n",
    "                      [('num', np.int64), ('key', 'S3')])\n",
    "output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "233e1f8e-16a1-48d1-a351-72575296e7e5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1312, 1534,  444])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output['num']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e9521fb-af1d-465f-9223-91bbe4669375",
   "metadata": {},
   "source": [
    "## numpy.fromstring(string, dtype=float, count=-1, *, sep, like=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2dba317e-99fe-4b6e-acf8-1ec5a84b06be",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.fromstring('1 2', dtype=int, sep=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b9ce87b7-77c3-448c-9a8e-2a334c43cf7a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.fromstring('1, 2', dtype=int, sep=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "401ba90b-1377-431d-b932-b1b8720954b5",
   "metadata": {},
   "source": [
    "## ndarray.tofile(fid, sep='', format='%s')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7f7bda3-3ef9-4589-87ee-7a532bc09897",
   "metadata": {},
   "source": [
    "## ndarray.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e5dc2afc-c0dc-4f0c-ad06-617df9664bbf",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.uint32([1, 2])\n",
    "a_list = list(a)\n",
    "a_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "828273cd-60bc-4688-b094-7112b12be68f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.uint32"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a_list[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "34e3ba51-249c-4b7b-94f8-4c7eb2977ba6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_tolist = a.tolist()\n",
    "a_tolist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "3c1d939d-8852-4eb4-a0b7-141957926d68",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a_tolist[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "88037b61-d7f5-44d7-8aa7-d41f82edfeb6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([1, 2]), array([3, 4])]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2], [3, 4]])\n",
    "list(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "61ac4e42-e524-4722-8ab7-b2691a638910",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2], [3, 4]]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d7b2789b-15e0-4aeb-82d3-3bce7a8d6765",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2], [3, 4]]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.tolist()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b563bba-8039-4e01-adcf-10f4a5ebc8b1",
   "metadata": {},
   "source": [
    "# 原始二进制文件\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "fromfile(file[, dtype, count, sep, offset])|从文本或二进制文件中的数据构造数组。\n",
    "ndarray.tofile(fid[, sep, format])|将数组以文本或二进制形式写入文件(默认)。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08274295-ec32-489c-bd78-2309f2a73bb8",
   "metadata": {},
   "source": [
    "## numpy.fromfile(file, dtype=float, count=-1, sep='', offset=0, *, like=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ecc36663-3798-48b6-8a2b-0937daabd384",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([((10, 0), 98.25)],\n",
       "      dtype=[('time', [('min', '<i8'), ('sec', '<i8')]), ('temp', '<f8')])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = np.dtype([('time', [('min', np.int64), ('sec', np.int64)]),\n",
    "               ('temp', float)])\n",
    "x = np.zeros((1,), dtype=dt)\n",
    "x['time']['min'] = 10; x['temp'] = 98.25\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "59599086-af86-4f33-8311-5a1dcceb0db7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([((10, 0), 98.25)],\n",
       "      dtype=[('time', [('min', '<i8'), ('sec', '<i8')]), ('temp', '<f8')])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fname = tempfile.mkstemp()[1]\n",
    "x.tofile(fname)\n",
    "np.fromfile(fname, dtype=dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "ebd49dd9-c0d2-48ee-be1f-619082925045",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([((10, 0), 98.25)],\n",
       "      dtype=[('time', [('min', '<i8'), ('sec', '<i8')]), ('temp', '<f8')])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.save(fname, x)\n",
    "np.load(fname + '.npy')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f34b86e2-8623-4634-9b94-694f966ada73",
   "metadata": {},
   "source": [
    "## ndarray.tofile(fid, sep='', format='%s')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53a45261-1a13-46f3-8f67-583e873bdb64",
   "metadata": {},
   "source": [
    "# 字符串格式\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "array2string(a[, max_line_width, precision, …])|返回数组的字符串表示形式。\n",
    "array_repr(arr[, max_line_width, precision, …])|返回数组的字符串表示形式。\n",
    "array_str(a[, max_line_width, precision, …])|返回数组中数据的字符串表示形式。\n",
    "format_float_positional(x[, precision, …])|将浮点标量格式化为位置表示法中的十进制字符串。\n",
    "format_float_scientific(x[, precision, …])|将浮点标量格式化为科学记数法中的十进制字符串。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d690dca6-f1b8-4f77-ae86-5cf90f27eb5b",
   "metadata": {},
   "source": [
    "## numpy.array2string(a, max_line_width=None, precision=None, suppress_small=None, separator=' ', prefix='', style=<no value>, formatter=None, threshold=None, edgeitems=None, sign=None, floatmode=None, suffix='', *, legacy=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "d0be675a-4568-4247-939c-1928dfe21b53",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[0.,1.,2.,3.]'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1e-16,1,2,3])\n",
    "np.array2string(x, precision=2, separator=',',\n",
    "                      suppress_small=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ea5fe341-298d-49f2-ace4-d7dd7c6bf9ea",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[0.00 1.00 2.00]'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x  = np.arange(3.)\n",
    "np.array2string(x, formatter={'float_kind':lambda x: \"%.2f\" % x})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "3867823b-8ef6-453b-be12-ead0ee54f532",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[0x0 0x1 0x2]'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x  = np.arange(3)\n",
    "np.array2string(x, formatter={'int':lambda x: hex(x)})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9aed223a-dbdc-4e92-b3d0-2c6b1a11b9ad",
   "metadata": {},
   "source": [
    "## numpy.array_repr(arr, max_line_width=None, precision=None, suppress_small=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "81ab9c91-0054-4ab4-bca0-4c5e73c7a907",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'array([1, 2])'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array_repr(np.array([1,2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "89afbb2c-1751-4a30-8671-846cafb194c6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'MaskedArray([0.])'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array_repr(np.ma.array([0.]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "44d5f99c-20ce-4eeb-b293-cc25e771ee3f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'array([], dtype=int32)'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array_repr(np.array([], np.int32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "57eaeb8c-2b17-4fe0-9e9e-0e2c870cff94",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'array([0.000001, 0.      , 2.      , 3.      ])'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1e-6, 4e-7, 2, 3])\n",
    "np.array_repr(x, precision=6, suppress_small=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6184688a-2eea-4a99-9584-b8e9e1b083cb",
   "metadata": {},
   "source": [
    "## numpy.array_str(a, max_line_width=None, precision=None, suppress_small=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "607f1a14-5416-44c3-873b-71dd93611979",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[0 1 2]'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array_str(np.arange(3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "786b512c-c556-4850-8fec-41d5fd2732b3",
   "metadata": {},
   "source": [
    "## numpy.format_float_positional(x, precision=None, unique=True, fractional=True, trim='k', sign=False, pad_left=None, pad_right=None, min_digits=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e564396c-ecf0-477a-b982-d7ca38efed93",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.1415927'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.format_float_positional(np.float32(np.pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "6d82b07e-675f-4839-8231-f5b661ecc2d7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.14'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.format_float_positional(np.float16(np.pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "3d99bd2e-e64a-4e86-a56d-520fad009928",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.3'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.format_float_positional(np.float16(0.3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "81299348-8a50-4641-aafa-05e99ecbec40",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.3000488281'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.format_float_positional(np.float16(0.3), unique=False, precision=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42e35bdb-6c0e-410a-86b5-009177383b61",
   "metadata": {},
   "source": [
    "## numpy.format_float_scientific(x, precision=None, unique=True, trim='k', sign=False, pad_left=None, exp_digits=None, min_digits=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b1540f10-7d28-401a-9a65-9e3f6fed8a8a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.1415927e+00'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.format_float_scientific(np.float32(np.pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "dcb51e39-e060-4816-9a42-d964f7d0ff06",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.230000071797338e+24'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = np.float32(1.23e24)\n",
    "np.format_float_scientific(s, unique=False, precision=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "3c834558-29bc-4e77-aecf-796e0ea08b10",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.23e+0024'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.format_float_scientific(s, exp_digits=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38b55857-a96e-4d4b-b69e-66baa2dfdd86",
   "metadata": {},
   "source": [
    "# 内存映射文件\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "memmap|创建存储在磁盘上二进制文件中的阵列的内存映射。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "001c5b61-58f7-4eb7-8252-8238a84d7368",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "data = np.arange(12, dtype='float32')\n",
    "data.resize((3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "6ca92b04-17c9-4983-841d-78bdda3dbc95",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "memmap([[0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.]], dtype=float32)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tempfile import mkdtemp\n",
    "import os.path as path\n",
    "filename = path.join(mkdtemp(), 'newfile.dat')\n",
    "fp = np.memmap(filename, dtype='float32', mode='w+', shape=(3,4))\n",
    "fp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "7da741f6-27d3-478d-bdb7-f563bba260cb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "memmap([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  6.,  7.],\n",
       "        [ 8.,  9., 10., 11.]], dtype=float32)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fp[:] = data[:]\n",
    "fp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "390bf235-a11d-4292-9aa2-e1e53ba98cbb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fp.filename == path.abspath(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "200424b1-b320-4bb4-8dc7-03aee3a7176a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fp.flush()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "6605112c-9fe7-48f4-88f1-97c76e397e1f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "memmap([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  6.,  7.],\n",
       "        [ 8.,  9., 10., 11.]], dtype=float32)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "newfp = np.memmap(filename, dtype='float32', mode='r', shape=(3,4))\n",
    "newfp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "2830b1e7-0448-4245-abd9-cf5974ee81ce",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fpr = np.memmap(filename, dtype='float32', mode='r', shape=(3,4))\n",
    "fpr.flags.writeable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "80014a4e-a224-455c-a2bf-b65ad45f9170",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fpc = np.memmap(filename, dtype='float32', mode='c', shape=(3,4))\n",
    "fpc.flags.writeable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "cd6eb15d-3220-4800-8c08-cf1ecdee86dc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "memmap([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  6.,  7.],\n",
       "        [ 8.,  9., 10., 11.]], dtype=float32)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fpc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "2af1dbcd-bc3c-4e8d-80b2-15d933443626",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "memmap([[ 0.,  0.,  0.,  0.],\n",
       "        [ 4.,  5.,  6.,  7.],\n",
       "        [ 8.,  9., 10., 11.]], dtype=float32)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fpc[0,:] = 0\n",
    "fpc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "ecc46c8f-aa65-4fdd-9e78-49bb9db060f2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "memmap([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  6.,  7.],\n",
       "        [ 8.,  9., 10., 11.]], dtype=float32)"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fpr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d3d8d315-ee56-4ef1-9116-03d34457e570",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "memmap([ 4.,  5.,  6.,  7.,  8.,  9., 10., 11.], dtype=float32)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fpo = np.memmap(filename, dtype='float32', mode='r', offset=16)\n",
    "fpo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4064db1-baa9-4406-b190-5433000f764b",
   "metadata": {},
   "source": [
    "# 文本格式选项\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "set_printoptions([precision, threshold, …])|设置打印选项。\n",
    "get_printoptions()|返回当前打印选项。\n",
    "set_string_function(f[, repr])|设置在更好的打印数组时要使用的Python函数。\n",
    "printoptions(*args, **kwargs)|上下文管理器，用于设置打印选项。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "71f9483a-8914-4a5d-94b7-f92cc1c14eed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.1235])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(precision=4)\n",
    "np.array([1.123456789])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "6e0c6290-2564-4425-96c5-64ccb55386c9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, ..., 7, 8, 9])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(threshold=5)\n",
    "np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "b799f6a4-e28d-4b63-8086-9c39fd8d778d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-4.9304e-32, -4.4409e-16,  0.0000e+00,  0.0000e+00])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eps = np.finfo(float).eps\n",
    "x = np.arange(4.)\n",
    "x**2 - (x + eps)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "351cced1-9eab-4e80-bba8-33a79213e16b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0., -0.,  0.,  0.])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(suppress=True)\n",
    "x**2 - (x + eps)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "88712a28-4c7b-4583-861d-3d42e2e1a34a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([int: 0, int: -1, int: -2])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(formatter={'all':lambda x: 'int: '+str(-x)})\n",
    "x = np.arange(3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "0b0c5c15-32e3-48e8-9d1f-96cf13c8f618",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions()\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "42331209-1c6c-488f-b028-658f6453ae09",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "np.set_printoptions(edgeitems=3, infstr='inf',\n",
    "    linewidth=75, nanstr='nan', precision=8,\n",
    "    suppress=False, threshold=1000, formatter=None)\n",
    "with np.printoptions(precision=2, suppress=True, threshold=5):\n",
    "    np.linspace(0, 10, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a031cb80-7f13-4af2-b28d-6e23bd020f92",
   "metadata": {},
   "source": [
    "## numpy.get_printoptions()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "068d057b-9cfa-4766-8c34-912286cfb0c9",
   "metadata": {},
   "source": [
    "## numpy.printoptions(*args, **kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "82baaa5c-37f1-4ad0-a68f-979d33d490df",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from numpy.testing import assert_equal\n",
    "with np.printoptions(precision=2):\n",
    "    np.array([2.0]) / 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "1127b540-96c0-4523-be0e-1cfd1c9cdacd",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "with np.printoptions(precision=2) as opts:\n",
    "     assert_equal(opts, np.get_printoptions())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d358720-0f96-4469-b847-3d7f479efe01",
   "metadata": {},
   "source": [
    "# 基数n表示\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "binary_repr(num[, width])|以字符串形式返回输入数字的二进制表示形式。\n",
    "base_repr(number[, base, padding])|返回给定基本系统中数字的字符串表示形式。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2c3217d-f9d0-4b31-9d4d-9605d13c3cc3",
   "metadata": {},
   "source": [
    "## numpy.binary_repr(num, width=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "fd2b7a51-05bb-4f57-bfbe-f17624ddf177",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'11'"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.binary_repr(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "51657136-1bc5-4cfc-a5f1-83bb2bf936b8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'-11'"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.binary_repr(-3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "368a2d15-02e6-4428-b524-ce1a467f45e6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0011'"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.binary_repr(3, width=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "5c1d2d59-d344-4886-966f-a35aa4028750",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'101'"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.binary_repr(-3, width=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "bd6bbe9f-690b-4e1b-a644-8129c9c48ed5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'11101'"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.binary_repr(-3, width=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6c02851-9f52-4059-a2df-8e27e065c20b",
   "metadata": {},
   "source": [
    "## numpy.base_repr(number, base=2, padding=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "a3aabcd8-d2ad-4a64-8842-c6e22818eb29",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'101'"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.base_repr(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "39eb11c1-1bdc-4676-b4d5-d8261c3db848",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'11'"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.base_repr(6, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "f224eb7e-a833-4adf-bc68-56eaf79f7650",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'00012'"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.base_repr(7, base=5, padding=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "4c5ac27e-92ec-41a3-8092-23dd6457fc45",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'A'"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.base_repr(10, base=16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "ba52a6cb-b8f2-4ac3-b041-6fddd941fd94",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'20'"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.base_repr(32, base=16)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca5106a6-1621-4555-8bfd-1be8561d3890",
   "metadata": {},
   "source": [
    "# 数据源\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "DataSource([destpath])|通用数据源文件（file、http、ftp等）。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d1a4718-7e74-4cd8-9a0c-649b80020a94",
   "metadata": {},
   "source": [
    "# 二进制格式描述\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "lib.format|二进制序列化"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc-autonumbering": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
